package com.xk.Reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;

public class PersonReflectTest1 {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
//        Person person=new Person("小张","男",19);
        //获取Person类的Class对象
        Class<?> clazz=com.xk.Reflect.Person.class;
        //获取Class对象的成员属性
        System.out.println("获取Class对象的成员属性:");
        Field[] fields=clazz.getDeclaredFields();
        for (Field f:fields){
            System.out.println(f);
        }
        //获取Class对象的成员方法
        System.out.println("获取Class对象的成员方法:");
        Method[] methods=clazz.getMethods();
        Method[] methods1=clazz.getDeclaredMethods();
        System.out.println("1、getMethods:");
        for (Method m:methods){
            System.out.println(m);
        }
        System.out.println("2、getDeclaredMethods:");
        for (Method m:methods1){
            System.out.println(m);
        }

        //获取Class对象的构造方法
        System.out.println("获取Class对象的构造方法:");
        Constructor[] constructors=clazz.getDeclaredConstructors();
        for (Constructor c:constructors){
            System.out.println(c);
        }

        // 创建Person类的实例并调用其方法
Person obj = (Person) clazz.newInstance();
obj.setName("刘德华");
obj.setSex("男");
obj.setBirthday(1965-10-10); // 使用 LocalDate

// 获取类的方法
Method[] methods2 = obj.getClass().getMethods();

// 调用类的方法
try {
    Method method = obj.getClass().getMethod("methodName", String.class, String.class); // 假设方法名为 "methodName"
    Object o = method.invoke(obj, "东方明珠", "粤语版");
    if (o != null) {
        System.out.println(o.toString());
    }
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
    e.printStackTrace();
}

    }
}
